import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:message_tools/application.dart';
import 'package:message_tools/components/appbar_plugins.dart';
import 'package:message_tools/components/index.dart';
import 'package:message_tools/store/index.dart';
import 'package:message_tools/store/view_model/index.dart';
import 'package:message_tools/ui/page/sms/sms_resend_page.dart';

import 'send_result_list_page.dart';

const buttonHeight = 50.0;
const rightButtonHeight = 40.0;

/// ListView 默认的实例,无状态
class SendResultPage extends StatefulWidget {
  SendResultPage();

  @override
  _SendResultPageState createState() => _SendResultPageState();
}

class _SendResultPageState extends State<SendResultPage> {
  @override
  void initState() {
    super.initState();
  }

  Widget build(BuildContext context) {
    var store = Store.value<SmsSendRecordModel>(context);
    store.loadSendCount();

    return Scaffold(
      appBar: AppBarPlugin.Create("新增发送任务", centerTitle: true,
          leading: Builder(builder: (BuildContext c) {
        return IconButton(
            icon: const Icon(Icons.arrow_back),
            onPressed: () {
              Navigator.pop(context);
            });
      })),
      body: Theme.of(context).platform == TargetPlatform.iOS
          ? CupertinoScrollbar(
              child: _buildBody(),
            )
          : Scrollbar(
              child: _buildBody(),
            ),
      resizeToAvoidBottomPadding: false,
    );
  }

  Widget _buildBody() {
    return SingleChildScrollView(
        child: Column(
      children: <Widget>[
        createNotSendView(),
        createSentView(),
        createSuccessView(),
        createFailView(),
      ],
    ));
  }

  Container createSentView() {
    return Container(
        margin: EdgeInsets.only(left: 10, right: 10),
        child: Row(
          children: <Widget>[
            Expanded(child: Text("已发送")),
            Expanded(
                child: Text(
                    "${Store.value<SmsSendRecordModel>(context).sentCount}人")),
            Expanded(
                child: createButton(
                    text: "查看",
                    minWidth: 120,
                    height: 40,
                    backgroundColor: Colors.blue,
                    textColor: Colors.white,
                    onPressed: () {
                      var store = Store.value<SmsSendRecordModel>(context);
                      store.loadSentSms(0, 10000);

                      Navigator.push(
                        context,
                        new MaterialPageRoute(
                            builder: (context) => SendResultListPage("已发送列表")),
                      );
                    })),
            SizedBox(
              width: 5,
            ),
            Expanded(
                child: createButton(
                    text: "补发",
                    minWidth: 120,
                    height: 40,
                    backgroundColor: Colors.deepOrange,
                    textColor: Colors.white,
                    onPressed: () {
                      if (Store.value<SmsSendRecordModel>(context).sentCount <=
                          0) {
                        Application.mAlertDialog(context, "没有需要补发的记录！");
                        return;
                      }
                      Navigator.push(
                        context,
                        new MaterialPageRoute(
                            builder: (context) => SmsReSendPage(1)),
                      );
                    }))
          ],
        ));
  }

  Container createSuccessView() {
    return Container(
        margin: EdgeInsets.only(left: 10, right: 10),
        child: Row(
          children: <Widget>[
            Expanded(child: Text("发送成功")),
            Expanded(
                child: Text(
                    "${Store.value<SmsSendRecordModel>(context).sendSuccessCount}人")),
            Expanded(
                child: createButton(
                    text: "查看",
                    minWidth: 120,
                    height: 40,
                    backgroundColor: Colors.blue,
                    textColor: Colors.white,
                    onPressed: () {
                      var store = Store.value<SmsSendRecordModel>(context);
                      store.loadSendSuccessSms(0, 10000);

                      Navigator.push(
                        context,
                        new MaterialPageRoute(
                            builder: (context) => SendResultListPage("发送成功列表")),
                      );
                    })),
            SizedBox(
              width: 5,
            ),
            Expanded(
                child: createButton(
                    text: "补发",
                    minWidth: 120,
                    height: 40,
                    backgroundColor: Colors.deepOrange,
                    textColor: Colors.white,
                    onPressed: () {
                      if (Store.value<SmsSendRecordModel>(context)
                              .sendSuccessCount <=
                          0) {
                        Application.mAlertDialog(context, "没有需要补发的记录！");
                        return;
                      }
                      Navigator.push(
                        context,
                        new MaterialPageRoute(
                            builder: (context) => SmsReSendPage(1)),
                      );
                    }))
          ],
        ));
  }

  Container createFailView() {
    return Container(
        margin: EdgeInsets.only(left: 10, right: 10),
        child: Row(
          children: <Widget>[
            Expanded(child: Text("发送失败")),
            Expanded(
                child: Text(
                    "${Store.value<SmsSendRecordModel>(context).sendFailCount}人")),
            Expanded(
                child: createButton(
                    text: "查看",
                    minWidth: 120,
                    height: 40,
                    backgroundColor: Colors.blue,
                    textColor: Colors.white,
                    onPressed: () {
                      var store = Store.value<SmsSendRecordModel>(context);
                      store.loadSendFailSms(0, 10000);
                      Navigator.push(
                        context,
                        new MaterialPageRoute(
                            builder: (context) => SendResultListPage("发送失败列表")),
                      );
                    })),
            SizedBox(
              width: 5,
            ),
            Expanded(
                child: createButton(
                    text: "补发",
                    minWidth: 120,
                    height: 40,
                    backgroundColor: Colors.deepOrange,
                    textColor: Colors.white,
                    onPressed: () {
                      if (Store.value<SmsSendRecordModel>(context)
                              .sendFailCount <=
                          0) {
                        Application.mAlertDialog(context, "没有需要补发的记录！");
                        return;
                      }

                      Navigator.push(
                        context,
                        new MaterialPageRoute(
                            builder: (context) => SmsReSendPage(3)),
                      );
                    }))
          ],
        ));
  }

  Container createNotSendView() {
    return Container(
        margin: EdgeInsets.only(left: 10, right: 10),
        child: Row(
          children: <Widget>[
            Expanded(child: Text("未发送")),
            Expanded(
                child: Text(
                    "${Store.value<SmsSendRecordModel>(context).notSendCount}人")),
            Expanded(
                child: createButton(
                    text: "查看",
                    minWidth: 120,
                    height: 40,
                    backgroundColor: Colors.blue,
                    textColor: Colors.white,
                    onPressed: () {
                      var store = Store.value<SmsSendRecordModel>(context);
                      store.loadNotSendSms(0, 10000);

                      Navigator.push(
                        context,
                        new MaterialPageRoute(
                            builder: (context) => SendResultListPage("未发送列表")),
                      );
                    })),
            SizedBox(
              width: 5,
            ),
            Expanded(
                child: createButton(
                    text: "补发",
                    minWidth: 120,
                    height: 40,
                    backgroundColor: Colors.deepOrange,
                    textColor: Colors.white,
                    onPressed: () {
                      if (Store.value<SmsSendRecordModel>(context)
                              .notSendCount <=
                          0) {
                        Application.mAlertDialog(context, "没有需要补发的记录！");
                        return;
                      }
                      Navigator.push(
                        context,
                        new MaterialPageRoute(
                            builder: (context) => SmsReSendPage(0)),
                      );
                    })),
          ],
        ));
  }
}
